package sqlexplorer.ui.views;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.part.ViewPart;

import sqlexplorer.application.ConnectionsController;
import sqlexplorer.application.exceptions.ConnectionFailedException;

public class ConnectionsView extends ViewPart implements Observer,IDoubleClickListener,ISelectionChangedListener{
	
	public static final String ID = "sqlexplorer.ui.views.connectionsview";
	
	private TableViewer m_connectionsTableViewer;
	
	private class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			if (parent instanceof Object[]) {
				return (Object[]) parent;
			}
	        return new Object[0];
		}
	}

	private class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return null;
		}

		public Image getImage(Object obj) {
			return null;
		}
	}
	
	public TableViewer getViewer() {
		return m_connectionsTableViewer;
	}
	
	public void createPartControl(Composite parent){
		m_connectionsTableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		
		m_connectionsTableViewer.setContentProvider(new ViewContentProvider());
		m_connectionsTableViewer.setLabelProvider(new ViewLabelProvider());
		
		ConnectionsController.getInstance().addObserver(this);
		m_connectionsTableViewer.setInput(ConnectionsController.getInstance().getConnectionNames());
		
		//m_connectionsTableViewer.addSelectionChangedListener(this);
		m_connectionsTableViewer.addDoubleClickListener(this);

		MenuManager menuManager = new MenuManager();
		Menu menu = menuManager.createContextMenu(m_connectionsTableViewer.getTable());
		
		m_connectionsTableViewer.getTable().setMenu(menu);
		getSite().registerContextMenu("SQLExplorer.contextmenu",menuManager, m_connectionsTableViewer);
		
		getSite().setSelectionProvider(m_connectionsTableViewer);
		
		/*
		try {
			getViewSite().getPage().openEditor(new SQLEditorInput(""), SQLEditor.ID);
		}catch (Exception e) {
			e.printStackTrace();
		}
		**/
	}
	
	public void setFocus(){
		m_connectionsTableViewer.getControl().setFocus();
	}
	
	public void addItem(){
		String input = new Date().toString();
		m_connectionsTableViewer.setInput(input);
	}
	
	@Override
	public void update(Observable o,Object arg){
		if(o instanceof ConnectionsController){
			String argString=(String)arg;
			
			if(argString.equals("connectionAdded")){
				m_connectionsTableViewer.setInput(ConnectionsController.getInstance().getConnectionNames());
			}
		}
	}
	
	@Override
	public void selectionChanged(SelectionChangedEvent event){
		IStructuredSelection selection=(IStructuredSelection)event.getSelection();
		
		if((selection != null) && (selection.isEmpty() == false)) {
			String firstObj =(String)selection.getFirstElement();
			
			if((firstObj != null) && (!firstObj.equals(""))) {
				try {
					ConnectionsController.getInstance().connect(firstObj);
				} catch (ConnectionFailedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	public void doubleClick(DoubleClickEvent event){
		IStructuredSelection selection = (IStructuredSelection)event.getSelection();
		
		if((selection != null) && (selection.isEmpty() == false)) {
			String firstObj =(String)selection.getFirstElement();
			
			if((firstObj != null) && (!firstObj.equals(""))) {
				try {
					ConnectionsController.getInstance().connect(firstObj);
				} catch (ConnectionFailedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}